<template>
	<view>

		<!-- 自定义标题 -->
		<tn-custom>
			<block slot="left">
				<navigator class='action border-custom' open-type="navigateBack" :delta="1" hover-class="none"
					:style="'width:' + Custom.width + 'px;height:' + Custom.height + 'px;margin-left:calc(750rpx - ' + Custom.right + 'px)'">
					<text class='cuIcon-back'></text>
					<text class='cuIcon-homefill'></text>
				</navigator>
			</block>
		</tn-custom>

		<view class="container">
			<u-button type="primary" @click="getUserInfo()" text="获取个人文件目录" customStyle="margin-bottom:30rpx;width:95%">
			</u-button>
			<u-button :disabled="!hasInfo" type="success" customStyle="margin-bottom:30rpx;width:95%" @click="upload()"
				text="上传">
			</u-button>

			<scroll-view scroll-y="true" class="list" v-if="documentList[0]">
				<view class="listItem" v-for="(item,index) in documentList" :key="index">
					<view class="time">
						<view>时间:</view>
						<view>{{item.time}}</view>
					</view>
					<view class="name">
						<view>文件名:</view>
						<view class="content">{{item.name}}</view>
					</view>
					<u-button type="success" @click="down(item.fileName)"
						customStyle="width:100rpx;height:67rpx;position:absolute;right:130rpx;top:50%;transform: translateY(-50%);;color:#fff;font-size:24rpx">
						下载
					</u-button>
					<u-button type="error" @click="del(item.fileName)"
						customStyle="width:100rpx;height:67rpx;position:absolute;right:20rpx;top:50%;transform: translateY(-50%);;color:#fff;font-size:24rpx">
						删除
					</u-button>
				</view>
			</scroll-view>
			<view class="" style="text-align: center;" v-if="!documentList[0]&&hasInfo">
				暂无文件,请上传!
			</view>
		</view>
	</view>
</template>

<script>
	import tnCustom from '../component/colorui/components/TnCustom.vue'
	export default {
		components: {
			tnCustom
		},
		data() {
			return {
				Custom: this.Custom,
				hasInfo: false,
				documentList: []
			};
		},
		methods: {
			down(fileName) {
				const that = this
				uni.showLoading({
					title: '下载中'
				})
				console.log(`https://linbo.vip:8888/${that.$myData.signature}/${fileName}`)
				uni.downloadFile({
					url: `https://linbo.vip:8888/${that.$myData.signature}/${fileName}`,
					success: (res) => {
						// console.log(res)
						if (res.statusCode === 200) {
							uni.hideLoading()
							uni.saveFile({
								tempFilePath: res.tempFilePath,
								success: function(res) {
									console.log(res)
									uni.showModal({
										showCancel: false,
										'content': '文件保存至:' + res.savedFilePath
									})
								}
							});
						}
					},
					fail(err) {
						console.log(err)
					}
				});
			},
			del(fileName) {
				const that = this
				uni.request({
					url: `${that.$cloud}?signature=${that.$myData.signature}&fileName=${fileName}`,
					method: 'delete',
					success(res) {
						console.log(res)
						if (res.data.code === 200) {
							that.getData(that.$myData.signature)
							uni.showToast({
								icon: 'success',
								title: '删除成功'
							})
						} else {
							uni.showToast({
								icon: 'error',
								title: '删除失败'
							})
						}
					}
				})
			},
			getUserInfo() {
				if (this.$myData.signature) {
					//获取文件列表
					this.getData(this.$myData.signature)
					this.hasInfo = true
					return
				}
				let that = this
				uni.getUserProfile({
					desc: "weixin", // 指明用途,这个参数是必须的
					success: (res) => {
						that.$myData.nickName = res.userInfo.nickName
						that.$myData.avatarUrl = res.userInfo.avatarUrl
						// console.log(res.userInfo)
						that.hasInfo = true
					}
				})
				uni.getUserInfo({
					success: function(res) {
						that.$myData.iv = res.iv
						that.$myData.signature = res.signature
						that.getData(res.signature)
						// console.log(res)
					}
				})
			},
			formatTime(time) {
				var date = new Date(time)
				const Y = date.getFullYear() + '-'
				const M =
					(date.getMonth() + 1 < 10 ?
						'0' + (date.getMonth() + 1) :
						date.getMonth() + 1) + '-'
				const D = date.getDate() + ' '
				const h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'
				const m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':'
				const s = date.getSeconds() < 10 ? '0' + date.getSeconds() + ':' : date.getSeconds()
				return Y + M + D + h + m + s
			},
			getData(signature) {
				uni.showLoading({

				})
				const that = this
				uni.request({
					url: `${that.$cloud}?signature=${signature}`,
					method: 'get',
					success(res) {
						uni.hideLoading()
						if (res.data.code === 200) {
							console.log(res)
							that.documentList = []
							const result = res.data?.data
							if (!result) return
							result.forEach((item) => {
								const arr = item.split('-')
								const time = arr.shift()
								that.documentList.push({
									time: that.formatTime(+time),
									name: arr.join('-'),
									fileName: item
								})
							})
						}
					},
					fail(err) {
						console.log(err)
						uni.hideLoading()
					}
				})
			},
			upload() {
				console.log(this.$myData)
				const that = this
				// 微信聊天记录选择文件
				wx.chooseMessageFile({
					success(res) {
						console.log(res.tempFiles)
						uni.showLoading({})
						uni.uploadFile({
							url: that.$uploads,
							filePath: res.tempFiles[0].path,
							name: 'file',
							formData: {
								'signature': that.$myData.signature,
								'name': res.tempFiles[0].name
							},
							success: (uploadFileRes) => {
								uni.hideLoading(uploadFileRes.data)
								that.documentList = []
								// console.log(uploadFileRes.data)
								const res = JSON.parse(uploadFileRes.data).data
								// if (!res[0]) {
								// 	uni.showToast({
								// 		icon: 'none',
								// 		title: '暂无文件,请上传!'
								// 	})
								// 	return
								// }
								res.forEach((item) => {
									const arr = item.split('-')
									const time = arr.shift()
									that.documentList.push({
										time: that.formatTime(+time),
										name: arr.join('-'),
										fileName: item
									})
								})
								uni.showToast({
									icon: 'none',
									title: '上传成功'
								})
							},
							fail: (err) => {
								console.log(err)
							}
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		// padding: 20rpx 30rpx;
		// background-color: #ccc;
	}

	.container {
		// padding: 30rpx;

		.list {
			text-indent: 10rpx;
			height: calc(100vh - 480rpx);
			width: 100%;
			overfow-y: scroll;
			border: 1rpx solid #ccc;
			border-radius: 8rpx;

			.listItem {
				height: 150rpx;
				width: 100%;
				border-bottom: 1px solid #b3bccc;
				position: relative;
				background-color: #fff;

				.time {}

				.name {
					width: 70%;

					.content {
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						word-break: break-all;
					}
				}
			}
		}
	}
</style>
